package rss2;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.attachment.Attachment;
import com.gargoylesoftware.htmlunit.attachment.CollectingAttachmentHandler;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class HtmlUnitTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			// homePage();
			DownloadTest1();
			// homePageVerifier();
			// form2();
			// homePage_Firefox();
			// getElements();
			// download();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Test
	public static void DownloadTest1() throws Exception {

		final WebClient webClient = new WebClient();
		webClient.setJavaScriptEnabled(false);
		webClient.setCssEnabled(false);

		//String myURL1 = "http://code.google.com/p/nku-csc-440-540-spr2011/downloads/detail?name=FooClient.java&can=2&q=";
		String myURL1 = "http://www.ebookshare.net/web-development/Web-Accessibility-2008-RETAiL-eBOOk-rebOOk-12087.html";
		
		final HtmlPage page = webClient.getPage(myURL1);
		System.out.println(page.getTitleText());

		List<HtmlAnchor> anchors1 = page.getAnchors();
		HtmlAnchor download_link = null;
		//String anchor_text1 = "FooClient.java";
		String anchor_text1 = "Download This Torrent";

		for (HtmlAnchor anchor : anchors1) {
			if (anchor.asText().indexOf(anchor_text1) > -1) {
				download_link = anchor;
				break;
			}
		}

		System.out.println("LINK Name : " + download_link.asText());
		final Page clickResult = download_link.click();

		final List<Attachment> attachments = new ArrayList<Attachment>();
		webClient.setAttachmentHandler(new CollectingAttachmentHandler(attachments));

		final WebResponse attachmentResponse = clickResult.getWebResponse();
		final InputStream attachmentStream = attachmentResponse.getContentAsStream();

		OutputStream out = new FileOutputStream(new File("X:\\dev\\temp\\ttt.txt"));

		int read = 0;
		byte[] bytes = new byte[1024];

		while ((read = attachmentStream.read(bytes)) != -1) {
			out.write(bytes, 0, read);
		}
		attachmentStream.close();
		out.flush();
		out.close();

		attachments.clear();
		webClient.closeAllWindows();
		System.out.println(":: CLOSE Browser ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
	}

	@Test
	public static void download() throws Exception {

		final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_8);
		HtmlPage page = webClient
				.getPage("http://www.ebookshare.net/web-development/Web-Accessibility-2008-RETAiL-eBOOk-rebOOk-12087.html");

		List<HtmlAnchor> anchors1 = page.getAnchors();
		HtmlAnchor download_link = null;
		for (HtmlAnchor anchor : anchors1) {
			if (anchor.asText().indexOf("Download This Torrent") > -1) {
				download_link = anchor;
				break;
			}
		}
		// page = download_link.click();
		System.out.println(download_link.asText());
		System.out.println("END!");

		webClient.closeAllWindows();
	}

	@Test
	public static void homePageVerifier() throws Exception {

		final WebClient webClient = new WebClient();
		webClient.setJavaScriptEnabled(false);
		webClient.setCssEnabled(false);

		// String myURL1 = "http://sourceforge.net/projects/htmlunit/files/";
		String myURL1 = "http://code.google.com/p/nku-csc-440-540-spr2011/downloads/detail?name=FooClient.java&can=2&q=";
		String myURL2 = "http://www.ebookshare.net/web-development/Web-Accessibility-2008-RETAiL-eBOOk-rebOOk-12087.html";

		final HtmlPage page = webClient.getPage(myURL1);
		System.out.println(page.getTitleText());

		final String pageAsXml = page.asXml();
		System.out.println(pageAsXml.contains("<body class=\"composite\">"));

		final String pageAsText = page.asText();
		System.out.println(pageAsText.contains("Support for the HTTP and HTTPS protocols"));

		List<HtmlAnchor> anchors1 = page.getAnchors();
		HtmlAnchor download_link = null;

		String anchor_text1 = "FooClient.java";
		String anchor_text2 = "Download This Torrent";

		for (HtmlAnchor anchor : anchors1) {
			if (anchor.asText().indexOf(anchor_text1) > -1) {
				download_link = anchor;
				break;
			}
		}

		System.out.println("LINK Name : " + download_link.asText());
		final Page clickResult = download_link.click();

		final List<Attachment> attachments = new ArrayList<Attachment>();
		webClient.setAttachmentHandler(new CollectingAttachmentHandler(attachments));

		// assertEquals(1, attachments.size());
		// assertTrue(HtmlPage.class.isInstance(attachments.get(0).getPage()));
		final WebResponse attachmentResponse = clickResult.getWebResponse();
		final InputStream attachmentStream = attachmentResponse.getContentAsStream();

		System.out.println("TYPE 	: " + attachmentResponse.getContentType());
		System.out.println("STATUS 	: " + attachmentResponse.getStatusCode());
		System.out.println("URL		: " + attachmentResponse.getWebRequest().getUrl());

		// write the inputStream to a FileOutputStream
		OutputStream out = new FileOutputStream(new File("X:\\dev\\temp\\ttt.txt"));

		int read = 0;
		byte[] bytes = new byte[1024];

		while ((read = attachmentStream.read(bytes)) != -1) {
			out.write(bytes, 0, read);
		}
		attachmentStream.close();
		out.flush();
		out.close();

		attachments.clear();
		webClient.closeAllWindows();
		System.out.println(":: CLOSE Browser ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
	}

	@Test
	public static void homePage_Firefox() throws Exception {
		final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
		final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
		assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());

		webClient.closeAllWindows();
	}

	@Test
	public void getElements() throws Exception {
		final WebClient webClient = new WebClient();
		final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
		final HtmlDivision div = page.getHtmlElementById("2");
		final HtmlAnchor anchor = page.getAnchorByName("anchor_name");

		webClient.closeAllWindows();
	}

	@Test
	public void xpath() throws Exception {
		final WebClient webClient = new WebClient();
		final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");

		// get list of all divs
		final List<?> divs = page.getByXPath("//div");

		// get div which has a 'name' attribute of 'John'
		final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@name='John']").get(0);

		webClient.closeAllWindows();
	}

	@Test
	public void submittingForm() throws Exception {
		final WebClient webClient = new WebClient();

		// Get the first page
		final HtmlPage page1 = webClient.getPage("http://some_url");

		// Get the form that we are dealing with and within that form,
		// find the submit button and the field that we want to change.
		final HtmlForm form = page1.getFormByName("myform");

		final HtmlSubmitInput button = form.getInputByName("submitbutton");
		final HtmlTextInput textField = form.getInputByName("userid");

		// Change the value of the text field
		textField.setValueAttribute("root");

		// Now submit the form by clicking the button and get back the second
		// page.
		final HtmlPage page2 = button.click();

		webClient.closeAllWindows();
	}

	@Test
	public static void form2() throws Exception {

		WebClient wc = new WebClient();
		HtmlPage page = (HtmlPage) wc.getPage("http://www.google.com");
		HtmlForm form = page.getFormByName("f");
		HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("btnG");
		HtmlPage page2 = (HtmlPage) button.click();
	}

}
